package com.zero.dubbo.data;

import com.zero.dubbo.dao.IUserRpcService;
import com.zero.dubbo.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class UserRpcServiceTest implements CommandLineRunner {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IUserRpcService userRpcService;

    @Override
    public void run(String... args) throws Exception {

        try{
            User user = userRpcService.get(null);
            logger.info("[run][发起一次 Dubbo RPC 请求，获得用户为({})", user);
        }catch (Exception e){
            logger.error("[run][获得用户发生异常，信息为:[{}]", e.getMessage());
        }

        // 添加用户
        try {
            // 创建 UserAddDTO
            User addUser = new User();
            addUser.setName("yudaoyuanmayudaoyuanma"); // 故意把名字打的特别长，为了校验名字不通过
            addUser.setGender(null); // 不传递性别，为了校验性别不通过
            // 发起调用
            userRpcService.add(addUser);
            logger.info("[run][发起一次 Dubbo RPC 请求，添加用户为({})]", addUser);
        } catch (Exception e) {
            logger.error("[run][添加用户发生异常，信息为:[{}]", e.getMessage());
        }

    }
}
